#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    port = NULL;
    ui->setupUi(this);
    //vytvoreni sub widgetu na prepinani mezi modama
    create = new CreateMode(this);
    create->hide();
    play = new PlayMode(this);
    ui->gridLayout->addWidget(play);
    ui->gridLayout->addWidget(create);

    //konekty
    connect(ui->actionPlay_Mode, SIGNAL(triggered()), play, SLOT(show()));
    connect(ui->actionPlay_Mode, SIGNAL(triggered()), create, SLOT(hide()));
    connect(ui->actionCreate_Mode, SIGNAL(triggered()), create, SLOT(show()));
    connect(ui->actionCreate_Mode, SIGNAL(triggered()), play, SLOT(hide()));
    connect(ui->actionFull_Screen, SIGNAL(triggered()), this, SLOT(mujFullScreen()));

    connect(ui->actionSave_As, SIGNAL(triggered()), play, SLOT(savePlayListAs()));
    connect(ui->actionSave, SIGNAL(triggered()), play, SLOT(savePlayList()));
    connect(ui->actionAdd_to_PlayList, SIGNAL(triggered()), play, SLOT(addToPlayList()));
    connect(ui->actionOpen_Playlist, SIGNAL(triggered()), play, SLOT(openPlayList()));
    connect(ui->actionNew_Paly_List, SIGNAL(triggered()), play, SLOT(newPlayList()));
    connect(ui->actionOpen_Track, SIGNAL(triggered()), create, SLOT(loadTrack()));

    //nastaveni seiove linky
    if(!connectPanel(port, "D", "Yes"))
    {
        QMessageBox *msg = new QMessageBox(QMessageBox::Warning, QString("Connection problem"), QString("Connection to panle isn't posible. You can tray it later."), QMessageBox::Ok);
        msg->show();
    }
    else
    {
        play->setPort(port);
        create->setPort(port);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
    if(port != NULL)
    {
        delete port;
    }
}

bool MainWindow::connectPanel(QtSeriovyPort *port, QString dotaz, QString odpoved)
{
    /*QStringList list = SerialList.GetPortsList();
    for(int i =0; i < list.length(); i++)
    {
        if(port->openPort(list[i]) != ER_OTEVRENI_PORTU)
        {
            port->write(&dotaz);
            QString *str;
            str = NULL;
            port->read(str);
            if(*str == odpoved)
            {
                return true;
            }
        }
    }*/
    delete port;
    port = NULL;
    return false;
}

void MainWindow::mujFullScreen()
{
    if(ui->actionFull_Screen->isChecked() == true)
    {
        showFullScreen();
    }
    else
    {
        showNormal();
    }
}
